package system

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"jgyApi/server/global"
	"jgyApi/server/model/common/response"
	"jgyApi/server/model/system"
	systemReq "jgyApi/server/model/system/request"
	systemRes "jgyApi/server/model/system/response"
	"jgyApi/server/utils"
	"strconv"
)

type BrandApi struct{}
//
//  @Description: 获取品牌分类
//  @receiver b
//  @param c
//
func (b *BrandApi) Index (c *gin.Context)  {

	//todo:这个接口要对接一下返回值
	var ksp systemReq.KeySnPage
	var res systemRes.BrandIndexResponse
	res.Code = -200
	err := c.ShouldBind(&ksp)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(ksp, utils.KeySnPageVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if !utils.IsThrough(ksp.Key) {
		response.FailWithMessage("key不对", c)
		return
	}
	var sn system.Sn
	err = global.GVA_DB.Model(&system.Sn{}).Where("value=?", ksp.Sn).First(&sn).Error
	if err != nil {
		response.FailWithMessage("该sn不存在", c)
		return
	}
	if *sn.Batch == "" {
		response.FailWithMessage("没有数据", c)
		return
	}

	type brand struct {
		ID         int64  `gorm:"column:id;type:int(100) unsigned;primaryKey;autoIncrement:true;comment:ID" json:"id"`
		Name       string `gorm:"column:name;type:varchar(30);not null;comment:品牌名称" json:"name"`
		Status     string `gorm:"column:status;type:varchar(20);not null;default:normal;comment:品牌状态" json:"status"`
		Img        string `gorm:"column:img;type:varchar(255);not null;comment:品牌图片" json:"img"`
		Batch      string `gorm:"column:batch;type:varchar(255);not null;comment:批次" json:"batch"`
	}
	var bs []brand
	where := fmt.Sprintf(" find_in_set(%s,batch)", *sn.Batch)
	pageNum, limit := 18, 18
	page, _ := strconv.Atoi(ksp.Page)
	offset := (page-1) * pageNum
	err = global.GVA_DB.Model(&system.Brand{}).Where("status=?", "normal").Where("admin_id", sn.AdminID).Where(where).Offset(offset).Limit(limit).Find(&bs).Error
	if err != nil {
		response.FailWithMessage("没有数据", c)
		return
	}
	response.OkWithDetailed(bs, "success", c)
	return
}

//
//
//  @Description: 获取品牌分类2
//  @receiver b
//  @param c
//
func (b *BrandApi) App (c *gin.Context)  {
	var kspi systemReq.KeySnPageId
	err := c.ShouldBind(&kspi)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(kspi, utils.KeySnPageIdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if !utils.IsThrough(kspi.Key) {
		response.FailWithMessage("key不对", c)
		return
	}
	var sn system.Sn
	err = global.GVA_DB.Model(&system.Sn{}).Where("value=?", kspi.Sn).First(&sn).Error
	if err != nil {
		response.FailWithMessage("该sn不存在", c)
		return
	}
	if *sn.Batch == "" {
		response.FailWithMessage("没有数据", c)
		return
	}
	var app []system.App
	pageNum, limit := 15, 15
	page := kspi.Page
	offset := (page-1) * pageNum
	whereBatch := fmt.Sprintf(" find_in_set(%s, app_batch)", *sn.Batch)
	whereId := fmt.Sprintf(" find_in_set(%d, app_brand_id)", kspi.ID)

	err = global.GVA_DB.Model(&system.App{}).Where("app_status=?", "normal").Where("app_admin_id=?", sn.AdminID).Where(whereBatch).Where(whereId).Offset(offset).Limit(limit).Find(&app).Error
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	for k, v := range app {
		app[k].AppPackage = v.Bg
	}
	response.OkWithDetailed(app, "success", c)
	return
}

//
// Banner
//  @Description: 获取默认首页banner图
//  @receiver b
//  @param c
//
func (b *BrandApi) Banner(c *gin.Context) {
	var banner system.Banner
	err := global.GVA_DB.Model(&system.Banner{}).Order("id DESC").Find(&banner).Error

	if err != nil {
		response.FailWithMessage("没有数据", c)
		return
	}
	response.OkWithDetailed(banner, "success", c)
	return

}
